package com.wephone.Student.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.wephone.Student.R;
import com.wephone.Student.util.ShareReferenceUtil;
import com.wephone.Student.util.Util;

public class NewRegisterActivity1 extends BaseActivity{
	private EditText userNameEdit;
	private EditText passwordEdit;
	private EditText nickEdit;
	private String uPlatform;
	private String uPlatformid;
	private String gender;
	private String username;
	public LocationClient mLocationClient = null;
	public BDLocationListener myListener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_register_1);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			uPlatform = bundle.getString("uPlatform");
			uPlatformid = bundle.getString("uPlatformid");
			gender = bundle.getString("gender");
			username = bundle.getString("username");
		}
		init();
		mLocationClient = new LocationClient(this);
		// 设置定位条件
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true); // 是否打开GPS
		option.setCoorType("bd09ll"); // 设置返回值的坐标类型。
		option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级
		option.setProdName("LocationDemo"); // 设置产品线名称。强烈建议您使用自定义的产品线名称，方便我们以后为您提供更高效准确的定位服务。
		option.setScanSpan(10); // 设置定时定位的时间间隔。单位毫秒
		mLocationClient.setLocOption(option);

		// 注册位置监听器
		mLocationClient.registerLocationListener(new BDLocationListener() {

			@Override
			public void onReceiveLocation(BDLocation location) {
				// TODO Auto-generated method stub
				if (location == null) {
					return;
				}
				ShareReferenceUtil.saveLocationLatitude(
						getApplicationContext(), location.getLatitude()
								+ "");
				ShareReferenceUtil.saveLocationLontitude(
						getApplicationContext(),
						location.getLongitude() + "");
				
			}


		});
		mLocationClient.start();
		if (mLocationClient != null && mLocationClient.isStarted()){
			mLocationClient.requestLocation();
		}
	}
	
	private void init(){
		userNameEdit = (EditText)findViewById(R.id.username_edit);
		passwordEdit = (EditText)findViewById(R.id.password_edit);
		nickEdit = (EditText)findViewById(R.id.nick_edit);
		
		if(!TextUtils.isEmpty(uPlatform) && !TextUtils.isEmpty(uPlatformid)){
			String nowTime = System.currentTimeMillis()/1000+"";
			userNameEdit.setText(nowTime+"@fake.com");
			passwordEdit.setText(nowTime);
			if(!TextUtils.isEmpty(username)){
				nickEdit.setText(username);
			}
			findViewById(R.id.reg_layout_1).setVisibility(View.GONE);
			findViewById(R.id.reg_layout_2).setVisibility(View.GONE);
		}
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),LoginActivity.class));
				finish();
			}
		});
		
		findViewById(R.id.to_login_page_text).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(getApplicationContext(),LoginActivity.class));
				finish();
			}
		});
		
		final float scale = this.getResources().getDisplayMetrics().density;
		CheckBox checkBox = (CheckBox)findViewById(R.id.agree_rule_check);
		checkBox.setPadding(checkBox.getPaddingLeft() + (int)(12.0f * scale + 0.5f),
		        checkBox.getPaddingTop(),
		        checkBox.getPaddingRight(),
		        checkBox.getPaddingBottom());
		
		findViewById(R.id.btn_next_step).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(TextUtils.isEmpty(userNameEdit.getText().toString())){
					Toast.makeText(getApplicationContext(), "请输入您的电子邮箱", Toast.LENGTH_SHORT).show();
					return;
				}
				 if(!Util.checkEmail((userNameEdit.getText().toString()))){
					Toast.makeText(getApplicationContext(), "邮箱格式不正确", Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(passwordEdit.getText().toString())){
					Toast.makeText(getApplicationContext(), "请您设置密码", Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(nickEdit.getText().toString())){
					Toast.makeText(getApplicationContext(), "请输入您的昵称", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!((CheckBox)findViewById(R.id.agree_rule_check)).isChecked()){
					Toast.makeText(getApplicationContext(), "请同意《山楂僧用户协议》", Toast.LENGTH_SHORT).show();
					return;
				}
				Bundle bundle = new Bundle();
				bundle.putString("username", userNameEdit.getText().toString());
				bundle.putString("password", passwordEdit.getText().toString());
				bundle.putString("nick", nickEdit.getText().toString());
				bundle.putString("uPlatform", uPlatform);
				bundle.putString("uPlatformid", uPlatformid);
				bundle.putString("gender", gender);
				startActivity(new Intent(getApplicationContext(),NewRegisterActivity2.class).putExtras(bundle));
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			startActivity(new Intent(getApplicationContext(),LoginActivity.class));
			finish();
			return true; 
       }
       return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mLocationClient.stop();
	}
}
